package com.service.Authorization.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.service.Authorization.dao.*;
import com.service.Authorization.dto.*;
import com.service.Authorization.entity.*;
import com.service.Authorization.enums.ProgrammeTypeEnum;
import com.service.Authorization.service.PortalUserService;
import com.service.Authorization.service.ProgrammeInfoExamineService;
import com.service.Authorization.service.QyProgrammeApiBindService;
import com.service.config.constant.Constant;
import com.service.config.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.service.Authorization.enums.ProgrammeTypeEnum.get;
import static com.service.config.constant.Constant.*;

/**
 * 方案信息表(ProgrammeInfoExamine)表服务实现类
 *
 * @author makejava
 * @since 2021-08-10 16:14:30
 */
@Service("programmeInfoExamineService")
public class ProgrammeInfoExamineServiceImpl implements ProgrammeInfoExamineService {
    @Resource
    private ProgrammeInfoExamineDao programmeInfoExamineDao;
    @Resource
    private ManageProgrammeInfoDao manageProgrammeInfoDao;
    @Value("${spring.fileSeverDomain}")
    private String fileSeverDomain;
    @Value("${spring.nginxUrl}")
    private String nginxUrl;
    @Resource
    private ReviewRecordInfoDao reviewRecordInfoDao;
    @Resource
    private ProgrammePasswordLayoutDao programmePasswordLayoutDao;
    @Resource
    private ProgrammeButtonDao programmeButtonDao;
    @Resource
    private ManageInfoDao manageInfoDao;
    @Resource
    private PortalProgrammeInfoDao portalProgrammeInfoDao;
    @Resource
    private PortalUserService portalUserService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 服务对象
     */
    @Resource
    private QyProgrammeApiBindService qyProgrammeApiBindService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProgrammeInfoExamine queryById(String id) {
        return this.programmeInfoExamineDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ProgrammeInfoExamine> queryAllByLimit(int offset, int limit) {
        return this.programmeInfoExamineDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param programmeInfoExamine 实例对象
     * @return 实例对象
     */
    @Override
    public ProgrammeInfoExamine insert(ProgrammeInfoExamine programmeInfoExamine) {
        this.programmeInfoExamineDao.insert(programmeInfoExamine);
        return programmeInfoExamine;
    }

    /**
     * 修改数据
     *
     * @param programmeInfoExamine 实例对象
     * @return 实例对象
     */
    @Override
    public ProgrammeInfoExamine update(ProgrammeInfoExamine programmeInfoExamine) {
        this.programmeInfoExamineDao.update(programmeInfoExamine);
        return this.queryById(programmeInfoExamine.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        programmeButtonDao.deleteById(id);
        programmePasswordLayoutDao.deleteById(id);
        portalProgrammeInfoDao.deleteById(id,"");//删除绑定
        return this.programmeInfoExamineDao.deleteById(id) > 0;
    }

    /**
     * 提交审核
     * @param submitProgrammeInfoExamineDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result updateProgrammeInfoConfig(String userId, SubmitProgrammeInfoExamineDto submitProgrammeInfoExamineDto) throws Exception {
        ProgrammeInfoExamine programmeInfoExamine = ModelMapperUtil.strictMap(submitProgrammeInfoExamineDto, ProgrammeInfoExamine.class);
        programmeInfoExamine.setComment(JsonXMLUtils.obj2json(submitProgrammeInfoExamineDto.getComment()));
        programmeInfoExamine.setStatus(REVIEW_TIME);
        programmeInfoExamine.setReviewStatusDesc("审核中");
        programmeInfoExamine.setManageId(userId);
        programmeInfoExamine.setUpdateTime(new Date());
        ProgrammeButtonDto programmeButtonDto = submitProgrammeInfoExamineDto.getProgrammeButtonDto();
        if (programmeButtonDto != null) {
            ProgrammeButton programmeButton = ModelMapperUtil.strictMap(programmeButtonDto, ProgrammeButton.class);
            programmeButton.setManageId(userId);
            programmeButton.setProgrammeId(programmeInfoExamine.getId());
            if (programmeButtonDao.queryById(programmeInfoExamine.getId()) != null) {
                programmeButtonDao.update(programmeButton);
            }else {
                programmeButtonDao.insert(programmeButton);
            }
        }
        if (submitProgrammeInfoExamineDto.getProgrammePasswordLayoutDto() != null) {
            ProgrammePasswordLayout programmePasswordLayout = ModelMapperUtil.strictMap(submitProgrammeInfoExamineDto.getProgrammePasswordLayoutDto(), ProgrammePasswordLayout.class);
            programmePasswordLayout.setManageId(userId);
            programmePasswordLayout.setProgrammeId(programmeInfoExamine.getId());
            programmeInfoExamine.setPassword(programmePasswordLayout.getType());
            if (programmePasswordLayoutDao.queryById(programmeInfoExamine.getId()) != null) {
                programmePasswordLayoutDao.update(programmePasswordLayout);
            }else {
                programmePasswordLayoutDao.insert(programmePasswordLayout);
            }
        }else {
            programmeInfoExamine.setPassword(false);
        }
        programmeInfoExamineDao.update(programmeInfoExamine);
        return Result.ok();
    }

    /**
     * 取消审核
     * @param programmeId
     * @return
     */
    @Override
    @Transactional
    public Result cancelProgrammeInfoConfig(String programmeId) {
        ProgrammeInfoExamine programmeInfoExamine = new ProgrammeInfoExamine();
        programmeInfoExamine.setId(programmeId);
        programmeInfoExamine.setStatus(REVIEW_WAIT);
        programmeInfoExamine.setUpdateTime(new Date());
        programmeInfoExamineDao.update(programmeInfoExamine);
        return Result.ok();
    }

    /**
     * 应用此方案
     * @param applicationProgrammeInfoDto
     * @return
     */
    @Override
    @Transactional
    public Result putIssue(ApplicationProgrammeInfoDto applicationProgrammeInfoDto) {
        ProgrammeInfoExamine programmeInfoExamine = programmeInfoExamineDao.queryById(applicationProgrammeInfoDto.getId());
        List<QyProgrammeApiBind> qyProgrammeApiBind = qyProgrammeApiBindService.queryById(applicationProgrammeInfoDto.getId(),applicationProgrammeInfoDto.getManageId());
        if (qyProgrammeApiBind == null || qyProgrammeApiBind.size() == 0 ){
            return Result.failure(NOT_BIND,NOT_BIND_DESC);
        }
        if (programmeInfoExamine.getStatus() == REVIEW_SUCCESS ) {
            programmeInfoExamine.setUpdateTime(new Date());
            ManageProgrammeInfo manageProgrammeInfo = ModelMapperUtil.strictMap(programmeInfoExamine, ManageProgrammeInfo.class);
            manageProgrammeInfo.setStatus(REVIEW_ISSUE);
            ManageProgrammeInfo manageProgrammeInfo1 = manageProgrammeInfoDao.queryById(manageProgrammeInfo.getId());
            if (manageProgrammeInfo1 != null) {
                manageProgrammeInfo.setStatus(REVIEW_ISSUE);
                manageProgrammeInfoDao.update(manageProgrammeInfo);
            }else {
                manageProgrammeInfoDao.insert(manageProgrammeInfo);
            }
            programmeInfoExamine.setStatus(REVIEW_ISSUE);
            programmeInfoExamine.setReviewStatusDesc("应用成功");
            programmeInfoExamineDao.update(programmeInfoExamine);
            return Result.ok();
        }
        return Result.failure(PARAMETER_NOT_FOUND,parameterNotFound);
    }

    /**
     * 查询方案信息或者查询预览唯一url
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public Result queryManageProgrammeInfo(String userId,String programmeInfoId,Integer type) {
        ProgrammeInfoExamine programmeInfoExamine = new ProgrammeInfoExamine();
        ProgrammeButton programmeButton = new ProgrammeButton();
        ProgrammePasswordLayout programmePasswordLayout = new ProgrammePasswordLayout();
        if (userId != null && !"".equals(userId)) {
            programmeInfoExamine.setManageId(userId);
            programmeButton.setManageId(userId);
            programmePasswordLayout.setManageId(userId);
        }
        if (type != null) {
            programmeInfoExamine.setProgrammeType(type);
        }
        List<ProgrammeInfoExamine> programmeInfoExamines = programmeInfoExamineDao.queryAll(programmeInfoExamine);
        List<ProgrammeButton> programmeButtons = programmeButtonDao.queryAll(programmeButton);
        List<ProgrammePasswordLayout> programmePasswordLayouts = programmePasswordLayoutDao.queryAll(programmePasswordLayout);
        List<ProgrammeInfoExamineDto> programmeInfoExamineDtoList = ModelMapperUtil.strictMapList(programmeInfoExamines, ProgrammeInfoExamineDto.class);
        if (programmeInfoExamines.size() > 0) {
            if (programmeInfoId  != null && !"".equals(programmeInfoId)) {
                programmeInfoExamine.setId(programmeInfoId);
            }
            programmeInfoExamines.forEach(programmeInfoExamine1 -> {
                List<ProgrammeInfoExamineDto> collect = programmeInfoExamineDtoList.stream().filter(programmeInfoExamineDto ->
                        programmeInfoExamineDto.getId().equals(programmeInfoExamine1.getId())).collect(Collectors.toList());
                collect.forEach(programmeInfoExamineDto -> {
                    programmeInfoExamineDto.setNewOne(false);
                    try {
                        Set<String> strings = redisUtil.redisLike(userId);
                        if(strings.stream().findFirst().isPresent()) {
                            String s = strings.stream().findFirst().get();
                            Object o = redisUtil.get(s);
                            UserDto  userDto = JsonXMLUtils.json2obj(o.toString(), UserDto.class);
                            programmeInfoExamineDto.setUserName(userDto.getUsername());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    programmeInfoExamineDto.setComment(JSONArray.parseArray(programmeInfoExamine1.getComment()).toJavaList(AssemblyInfoDto.class));
                    List<ProgrammeButton> collect1 = programmeButtons.stream().filter(programmeButton1 -> programmeButton1.getProgrammeId().equals(programmeInfoExamineDto.getId())).collect(Collectors.toList());
                    if (collect1.size() > 0) {
                        programmeInfoExamineDto.setProgrammeButton(ModelMapperUtil.strictMap(collect1.get(0),ProgrammeButtonDto.class));
                    }
                    List<ProgrammePasswordLayout> collect2 = programmePasswordLayouts.stream().filter(programmePasswordLayout1 -> programmePasswordLayout1.getProgrammeId().equals(programmeInfoExamineDto.getId())).collect(Collectors.toList());
                    if (collect2.size() > 0) {
                        programmeInfoExamineDto.setProgrammePasswordLayout(ModelMapperUtil.strictMap(collect2.get(0),ProgrammePasswordLayoutDto.class));
                    }
                });
            });
            HashMap map = new HashMap();
            if (type != null) {
                List<ProgrammeInfoExamineDto> collect = programmeInfoExamineDtoList.stream().filter(programmeInfoExamineDto -> programmeInfoExamineDto.getProgrammeType() == type).collect(Collectors.toList());
                ProgrammeTypeEnum programmeTypeEnum = ProgrammeTypeEnum.get(type);
                switch (programmeTypeEnum){
                    case QY:
                        map.put(Objects.requireNonNull(get(type)).getDescription(),collect);
                    case Min:
                        map.put(Objects.requireNonNull(get(type)).getDescription(),collect);
                    case common1:
                        map.put(Objects.requireNonNull(get(type)).getDescription(),collect);
                }
                return  Result.ok().setData(map);
            }
            List<ProgrammeInfoExamineDto> programmeInfoExamineDtoCommonList = new ArrayList<>();
            List<ProgrammeInfoExamineDto> programmeInfoExamineDtoQyList = new ArrayList<>();
            List<ProgrammeInfoExamineDto> programmeInfoExamineDtoMinList = new ArrayList<>();
            if (programmeInfoId != null && !"".equals(programmeInfoId) && programmeInfoId.length() ==16) {
                return  Result.ok().setData(programmeInfoExamineDtoList.stream().filter(programmeInfoExamineDto -> programmeInfoExamineDto.getId().equals(programmeInfoId)).collect(Collectors.toList()));
            }
            programmeInfoExamineDtoCommonList = programmeInfoExamineDtoList.stream().filter(programmeInfoExamineDto -> programmeInfoExamineDto.getProgrammeType() == 0).collect(Collectors.toList());
            programmeInfoExamineDtoQyList = programmeInfoExamineDtoList.stream().filter(programmeInfoExamineDto -> programmeInfoExamineDto.getProgrammeType() == 1).collect(Collectors.toList());
            programmeInfoExamineDtoMinList = programmeInfoExamineDtoList.stream().filter(programmeInfoExamineDto -> programmeInfoExamineDto.getProgrammeType() == 2).collect(Collectors.toList());
            map.put("common",programmeInfoExamineDtoCommonList);
            map.put("isQy",programmeInfoExamineDtoQyList);
            map.put("isMini",programmeInfoExamineDtoMinList);
            return  Result.ok().setData(map);
        }
        addDefaultProgrammeInfo(userId,type);
        return queryManageProgrammeInfo(userId,programmeInfoId,type);
    }
    @Override
    @Transactional
    public void addDefaultProgrammeInfo(String userId,Integer isHave) {
        ProgrammeInfoExamine programmeInfoExamine1 = programmeInfoExamineDao.queryById("1");
        programmeInfoExamine1.setId(IdUtil.getStringId());
        programmeInfoExamine1.setProgrammeType(isHave);
        ProgrammeButton programmeButton2 = programmeButtonDao.queryById("1");
        programmeButton2.setManageId(userId);
        programmeButton2.setProgrammeId(programmeInfoExamine1.getId());
        programmeInfoExamine1.setType(false);
        ProgrammePasswordLayout programmePasswordLayout1 = programmePasswordLayoutDao.queryById("1");
        programmePasswordLayout1.setProgrammeId(programmeInfoExamine1.getId());
        programmePasswordLayout1.setManageId(userId);
        programmeInfoExamine1.setManageId(userId);
        programmeInfoExamine1.setAddTime(new Date());
        programmeInfoExamine1.setStatus(REVIEW_WAIT);
        programmeInfoExamine1.setAllowDeviceNum(1);
        programmeInfoExamine1.setPasswordExpireTime(5);
        programmeInfoExamine1.setPasswordLength(6);
        programmeInfoExamine1.setAuthExpireTime("180");
        programmeInfoExamine1.setReviewStatusDesc("待审核");
        programmeInfoExamine1.setProgrammeName("新增".concat(programmeInfoExamine1.getProgrammeName()));
        programmeInfoExamine1.setUpdateTime(new Date());
        programmeInfoExamine1.setPortalUrl(fileSeverDomain.concat(nginxUrl).concat("/").concat(programmeInfoExamine1.getId()));
        programmeInfoExamineDao.insert(programmeInfoExamine1);
        programmeButtonDao.insert(programmeButton2);
        programmePasswordLayoutDao.insert(programmePasswordLayout1);
    }

    @Override
    public List<ProgrammeInfoExamine> queryAll(ProgrammeInfoExamine programmeInfoExamine) {
        return programmeInfoExamineDao.queryAll(programmeInfoExamine);
    }

    @Override
    @Transactional
    public Result getUrl(String userId,String programmeInfoId) {
        PortalProgrammeInfo portalProgrammeInfo = new PortalProgrammeInfo();
        portalProgrammeInfo.setProgrammeId(programmeInfoId);
        portalProgrammeInfo.setManageId(userId);
        List<PortalProgrammeInfo> portalProgrammeInfos = portalProgrammeInfoDao.queryAll(portalProgrammeInfo);
        List<ScreenUserDto> screenUserDtoList = portalUserService.queryAll(userId);
        if (portalProgrammeInfos.size() > 0) {
            List<PortalProgrammeInfoDto> portalProgrammeInfoDtoList = new ArrayList<>();
            portalProgrammeInfos.stream().forEach(portalProgrammeInfo1 -> {
                PortalProgrammeInfoDto portalProgrammeInfoDto = new PortalProgrammeInfoDto();
                portalProgrammeInfoDto.setProgrammeId(portalProgrammeInfo1.getProgrammeId());
                List<ScreenUserDto> collect = screenUserDtoList.stream().filter(screenUserDto ->
                        screenUserDto.getId()
                        .equals(portalProgrammeInfo1.getPortalUserId()))
                        .collect(Collectors.toList());
                if (collect.size() > 0) {
                    portalProgrammeInfoDto.setTenantName(collect.get(0).getTenantName());
                    portalProgrammeInfoDto.setTenantPwd(collect.get(0).getTenantPwd());
                    portalProgrammeInfoDto.setDomain(collect.get(0).getDomain());
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(fileSeverDomain).append(nginxUrl).append("/index.html").append("?programmeInfoId=").append(programmeInfoId).append("&portalUserId=").append(portalProgrammeInfo1.getPortalUserId());
                portalProgrammeInfoDto.setPortalUrl(stringBuilder.toString());
                portalProgrammeInfoDtoList.add(portalProgrammeInfoDto);
            });
            return Result.ok().setData(portalProgrammeInfoDtoList);
        }
        return Result.failure(API_NOT_BIND,API_NOT_BIND_DESC);
    }

    /**
     * 新增方案
     * @param userId
     * @param addProgrammeInfoExamineDto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Result addManageProgrammeInfo(String userId, AddProgrammeInfoExamineDto addProgrammeInfoExamineDto) throws Exception {
        ProgrammeInfoExamine programmeInfoExamine = ModelMapperUtil.strictMap(addProgrammeInfoExamineDto, ProgrammeInfoExamine.class);
        programmeInfoExamine.setId(IdUtil.getStringId());
        programmeInfoExamine.setManageId(userId);
        programmeInfoExamine.setType(false);
        programmeInfoExamine.setComment(JsonXMLUtils.obj2json(addProgrammeInfoExamineDto.getComment()));
        programmeInfoExamine.setPortalUrl(fileSeverDomain.concat(nginxUrl).concat("?").concat(programmeInfoExamine.getId()));
        programmeInfoExamine.setStatus(REVIEW_WAIT); // 待审核
        programmeInfoExamine.setReviewStatusDesc("待审核");
        programmeInfoExamine.setAddTime(new Date());
        programmeInfoExamine.setUpdateTime(new Date());
        ProgrammeButton programmeButton2 = addProgrammeInfoExamineDto.getProgrammeButton();
        programmeButton2.setManageId(userId);
        programmeButton2.setProgrammeId(programmeInfoExamine.getId());
        if (StringUtils.isEmpty(programmeButton2.getButtonY())) {
            programmeButton2.setButtonY("10%");
            programmeButton2.setButtonYOld("10%");
        }
        if (StringUtils.isEmpty(programmeButton2.getButtonColor() )) {
            programmeButton2.setButtonColor("#ff0");
            programmeButton2.setButtonColorOld("#ff0");
        }
        ProgrammePasswordLayout programmePasswordLayout1 = addProgrammeInfoExamineDto.getProgrammePasswordLayout();
        programmePasswordLayout1.setProgrammeId(programmeInfoExamine.getId());
        programmePasswordLayout1.setManageId(userId);
        if (programmePasswordLayout1.getType()) {
            programmeInfoExamine.setPassword(true);
        }else {
            programmeInfoExamine.setPassword(false);
        }
        if (StringUtils.isEmpty(programmePasswordLayout1.getPasswordYOld())) {
            programmePasswordLayout1.setPasswordYOld("30%");
            programmePasswordLayout1.setPasswordYNew("30%");
        }
        if (programmeInfoExamine.getAllowDeviceNum() == null) {
            programmeInfoExamine.setAllowDeviceNum(1);
        }if (programmeInfoExamine.getPasswordExpireTime() == null) {
            programmeInfoExamine.setPasswordExpireTime(5);
        }if (programmeInfoExamine.getPasswordLength() == null) {
            programmeInfoExamine.setPasswordLength(6);
        }if (StringUtils.isEmpty(programmeInfoExamine.getAuthExpireTime())) {
            programmeInfoExamine.setAuthExpireTime("180");
        }
        ManageProgrammeInfo manageProgrammeInfo = ModelMapperUtil.strictMap(programmeInfoExamine, ManageProgrammeInfo.class);
        programmeInfoExamineDao.insert(programmeInfoExamine);
        manageProgrammeInfoDao.insert(manageProgrammeInfo);
        programmeButtonDao.insert(programmeButton2);
        programmePasswordLayoutDao.insert(programmePasswordLayout1);
        return Result.ok();
    }

    /**
     * 查询审核中的数据
     * @param programmeInfoExamine
     * @param flag
     * @return
     */
    @Override
    public Result queryRecordInfoS(ProgrammeInfoExamine programmeInfoExamine,String flag) {
        List<ProgrammeInfoExamine> programmeInfoExamineList = new ArrayList<>();
        if (flag.equals("1")) { // 查询提交审核的店铺
            programmeInfoExamine.setStatus(Constant.REVIEW_TIME);
            programmeInfoExamine.setType(false);
            programmeInfoExamineList = programmeInfoExamineDao.queryAll(programmeInfoExamine);
        }else if (flag.equals("2")){
            // 查询审核成功和审核失败的店铺
            List<Integer> list = new ArrayList<>();
            list.add(2);
            list.add(3);
            List<ReviewRecordInfo> reviewRecordInfos = reviewRecordInfoDao.queryByReviewStatus(list);
            List<ManageInfo> manageInfos = manageInfoDao.queryAll(new ManageInfo());
            if (reviewRecordInfos.size() > 0) {
                List<RevordProgrammeInfoExamineDto> programmeInfoExamineDtos = ModelMapperUtil.strictMapList(reviewRecordInfos, RevordProgrammeInfoExamineDto.class);
                reviewRecordInfos.stream().forEach(programmeInfoExamine1 -> {
                    List<RevordProgrammeInfoExamineDto> collect = programmeInfoExamineDtos.stream().filter(programmeInfoExamineDto ->
                            programmeInfoExamineDto.getId().equals(programmeInfoExamine1.getId())).collect(Collectors.toList());
                    collect.forEach(programmeInfoExamineDto -> {
                        if (programmeInfoExamine1.getManageId() != null && !"".equals(programmeInfoExamine1.getManageId())) {
                            List<ManageInfo> collect1 = manageInfos.stream().filter(manageInfo1 -> manageInfo1.getId().equals(programmeInfoExamine1.getManageId())).collect(Collectors.toList());
                            programmeInfoExamineDto.setUserName(collect1.get(0).getUsername());
                        }
                        if (programmeInfoExamine1.getDetailedComment() != null && !"".equals(programmeInfoExamine1.getDetailedComment())) {
                            try {
                                VerifyProgrammeInfoDto verifyProgrammeInfoDto = JsonXMLUtils.json2obj(programmeInfoExamine1.getDetailedComment(), VerifyProgrammeInfoDto.class);
                                programmeInfoExamineDto.setDetailedComment(verifyProgrammeInfoDto);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    });
                });
                return Result.ok().setData(programmeInfoExamineDtos);
            }
            return Result.ok().setData(reviewRecordInfos);
        }else {
            List<ReviewRecordInfo> reviewRecordInfos = reviewRecordInfoDao.queryAll(new ReviewRecordInfo());
            List<ManageInfo> manageInfos = manageInfoDao.queryAll(new ManageInfo());
            if (reviewRecordInfos.size() > 0) {
                List<RevordProgrammeInfoExamineDto> programmeInfoExamineDtos = ModelMapperUtil.strictMapList(reviewRecordInfos, RevordProgrammeInfoExamineDto.class);
                reviewRecordInfos.stream().forEach(programmeInfoExamine1 -> {
                    List<RevordProgrammeInfoExamineDto> collect = programmeInfoExamineDtos.stream().filter(programmeInfoExamineDto ->
                            programmeInfoExamineDto.getId().equals(programmeInfoExamine1.getId())).collect(Collectors.toList());
                    collect.forEach(programmeInfoExamineDto -> {
                        if (programmeInfoExamine1.getManageId() != null && !"".equals(programmeInfoExamine1.getManageId())) {
                            List<ManageInfo> collect1 = manageInfos.stream().filter(manageInfo1 -> manageInfo1.getId().equals(programmeInfoExamine1.getManageId())).collect(Collectors.toList());
                            programmeInfoExamineDto.setUserName(collect1.get(0).getUsername());
                        }
                        if (programmeInfoExamine1.getDetailedComment() != null && !"".equals(programmeInfoExamine1.getDetailedComment())) {
                            try {
                                VerifyProgrammeInfoDto  verifyProgrammeInfoDto = JsonXMLUtils.json2obj(programmeInfoExamine1.getDetailedComment(), VerifyProgrammeInfoDto.class);
                                programmeInfoExamineDto.setDetailedComment(verifyProgrammeInfoDto);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    });
                });
                return Result.ok().setData(programmeInfoExamineDtos);
            }
            return Result.ok().setData(reviewRecordInfos);
        }
        if (programmeInfoExamineList.size() > 0) {
            List<ManageInfo> manageInfos = manageInfoDao.queryAll(new ManageInfo());
            List<ProgrammeButton> programmeButtons = programmeButtonDao.queryAll(new ProgrammeButton());
            List<ProgrammePasswordLayout> programmePasswordLayouts = programmePasswordLayoutDao.queryAll(new ProgrammePasswordLayout());
            List<ProgrammeInfoExamineDto> programmeInfoExamineDtos = ModelMapperUtil.strictMapList(programmeInfoExamineList, ProgrammeInfoExamineDto.class);
            programmeInfoExamineList.stream().forEach(programmeInfoExamine1 -> {
                List<ProgrammeInfoExamineDto> collect = programmeInfoExamineDtos.stream().filter(programmeInfoExamineDto ->
                        programmeInfoExamineDto.getId().equals(programmeInfoExamine1.getId())).collect(Collectors.toList());
                collect.forEach(programmeInfoExamineDto -> {
                    if (programmeInfoExamine1.getManageId() != null && !"".equals(programmeInfoExamine1.getManageId())) {
                        List<ManageInfo> collect1 = manageInfos.stream().filter(manageInfo1 -> manageInfo1.getId().equals(programmeInfoExamine1.getManageId())).collect(Collectors.toList());
                        programmeInfoExamineDto.setUserName(collect1.get(0).getUsername());
                    }
                    programmeInfoExamineDto.setComment(JSONArray.parseArray(programmeInfoExamine1.getComment()).toJavaList(AssemblyInfoDto.class));
                    List<ProgrammeButton> collect1 = programmeButtons.stream().filter(programmeButton1 -> programmeButton1.getProgrammeId().equals(programmeInfoExamineDto.getId())).collect(Collectors.toList());
                    if (collect1.size() > 0) {
                        programmeInfoExamineDto.setProgrammeButton(ModelMapperUtil.strictMap(collect1.get(0),ProgrammeButtonDto.class));
                    }
                    List<ProgrammePasswordLayout> collect2 = programmePasswordLayouts.stream().filter(programmePasswordLayout1 -> programmePasswordLayout1.getProgrammeId().equals(programmeInfoExamineDto.getId())).collect(Collectors.toList());
                    if (collect2.size() > 0) {
                        programmeInfoExamineDto.setProgrammePasswordLayout(ModelMapperUtil.strictMap(collect2.get(0),ProgrammePasswordLayoutDto.class));
                    }
                });
            });
            return Result.ok().setData(programmeInfoExamineDtos);
        }
        return Result.ok().setData(programmeInfoExamineList);
    }

    /**
     * 开始审核
     * @param cloudAuthToken
     * @param flag
     * @param verifyProgrammeInfoDto
     */
    @Override
    @Transactional
    public void verifyShopInfo(String cloudAuthToken, String flag, VerifyProgrammeInfoDto verifyProgrammeInfoDto) throws Exception {
        OpUserInfoDto opUserInfoDto = JsonXMLUtils.json2obj(cloudAuthToken, OpUserInfoDto.class);
        ProgrammeInfoExamine programmeInfoExamine = programmeInfoExamineDao.queryById(verifyProgrammeInfoDto.getProgrammeId());
        String s = JsonXMLUtils.obj2json(verifyProgrammeInfoDto);
        String id = IdUtil.getStringId(); // 获取审批号
        if (flag.equals("1")) { // 审核通过
            programmeInfoExamine.setId(verifyProgrammeInfoDto.getProgrammeId());
            programmeInfoExamine.setVersionNo(id);
            programmeInfoExamine.setStatus(Constant.REVIEW_SUCCESS); // 3--审核成功
            programmeInfoExamine.setUpdateTime(new Date());
            programmeInfoExamine.setDetailedComment(s);
            programmeInfoExamineDao.update(programmeInfoExamine);
            ReviewRecordInfo reviewRecordInfo = ModelMapperUtil.strictMap(programmeInfoExamine, ReviewRecordInfo.class);
            reviewRecordInfo.setId(IdUtil.getStringId());
            reviewRecordInfo.setProgrammeId(programmeInfoExamine.getId());
            reviewRecordInfo.setReviewer(opUserInfoDto.getUserName());
            reviewRecordInfoDao.insert(reviewRecordInfo);
        }else {
            programmeInfoExamine.setId(programmeInfoExamine.getId());
            programmeInfoExamine.setVersionNo(id);
            programmeInfoExamine.setStatus(Constant.REVIEW_FAIL); // 2--审核失败
            programmeInfoExamine.setUpdateTime(new Date());
            programmeInfoExamine.setDetailedComment(s);
            programmeInfoExamineDao.update(programmeInfoExamine);
            ReviewRecordInfo reviewRecordInfo = ModelMapperUtil.strictMap(programmeInfoExamine, ReviewRecordInfo.class);
            reviewRecordInfo.setId(IdUtil.getStringId());
            reviewRecordInfo.setProgrammeId(programmeInfoExamine.getId());
            reviewRecordInfo.setReviewer(opUserInfoDto.getUserName());
            reviewRecordInfoDao.insert(reviewRecordInfo);
        }
    }
}
